package javolution.xml;

import java.util.Hashtable;
import javolution.Javolution;
import javolution.text.CharArray;
import javolution.text.TextBuilder;
import javolution.text.TextFormat;
import javolution.xml.sax.Attributes;
import javolution.xml.stream.XMLStreamException;
import javolution.xml.stream.XMLStreamReader;
import javolution.xml.stream.XMLStreamReaderImpl;
import javolution.xml.stream.XMLStreamWriter;
import javolution.xml.stream.XMLStreamWriterImpl;

/* loaded from: classes2.dex */
public abstract class XMLFormat<T> {
    private static final String NULL = "Null";
    static volatile int _ClassInstancesLength;
    final Class _class;
    static volatile XMLFormat[] _ClassInstances = new XMLFormat[64];
    private static Hashtable _ClassToFormat = new Hashtable();

    /* loaded from: classes2.dex */
    public static final class InputElement {
        private XMLBinding _binding;
        private boolean _isReaderAtNext;
        final XMLStreamReaderImpl _reader = new XMLStreamReaderImpl();
        private XMLReferenceResolver _referenceResolver;

        /* JADX INFO: Access modifiers changed from: package-private */
        public InputElement() {
            reset();
        }

        private Object get(Class cls) throws XMLStreamException {
            XMLFormat<T> format = this._binding.getFormat(cls);
            this._isReaderAtNext = false;
            T newInstance = format.newInstance(cls, this);
            XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
            if (xMLReferenceResolver != null) {
                xMLReferenceResolver.createReference(newInstance, this);
            }
            format.read(this, newInstance);
            if (hasNext()) {
                throw new XMLStreamException("Incomplete element reading", this._reader.getLocation());
            }
            this._isReaderAtNext = false;
            return newInstance;
        }

        public <T> T get(String str) throws XMLStreamException {
            T t;
            if (hasNext() && this._reader.getLocalName().equals(str)) {
                XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
                if (xMLReferenceResolver == null || (t = (T) xMLReferenceResolver.readReference(this)) == null) {
                    return (T) get(this._binding.readClassAttribute(this._reader));
                }
                if (this._reader.next() == 2) {
                    this._isReaderAtNext = false;
                    return t;
                }
                throw new XMLStreamException("Non Empty Reference Element");
            }
            return null;
        }

        public <T> T get(String str, Class<T> cls) throws XMLStreamException {
            T t;
            if (hasNext() && this._reader.getLocalName().equals(str)) {
                XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
                if (xMLReferenceResolver == null || (t = (T) xMLReferenceResolver.readReference(this)) == null) {
                    return (T) get(cls);
                }
                if (this._reader.next() == 2) {
                    this._isReaderAtNext = false;
                    return t;
                }
                throw new XMLStreamException("Non Empty Reference Element");
            }
            return null;
        }

        public <T> T get(String str, String str2) throws XMLStreamException {
            T t;
            if (str2 == null) {
                return (T) get(str);
            }
            if (hasNext() && this._reader.getLocalName().equals(str) && this._reader.getNamespaceURI().equals(str2)) {
                XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
                if (xMLReferenceResolver == null || (t = (T) xMLReferenceResolver.readReference(this)) == null) {
                    return (T) get(this._binding.readClassAttribute(this._reader));
                }
                if (this._reader.next() == 2) {
                    this._isReaderAtNext = false;
                    return t;
                }
                throw new XMLStreamException("Non Empty Reference Element");
            }
            return null;
        }

        public <T> T get(String str, String str2, Class<T> cls) throws XMLStreamException {
            T t;
            if (str2 == null) {
                return (T) get(str, cls);
            }
            if (hasNext() && this._reader.getLocalName().equals(str) && this._reader.getNamespaceURI().equals(str2)) {
                XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
                if (xMLReferenceResolver == null || (t = (T) xMLReferenceResolver.readReference(this)) == null) {
                    return (T) get(cls);
                }
                if (this._reader.next() == 2) {
                    this._isReaderAtNext = false;
                    return t;
                }
                throw new XMLStreamException("Non Empty Reference Element");
            }
            return null;
        }

        public char getAttribute(String str, char c) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            if (attribute == null) {
                return c;
            }
            if (attribute.length() == 1) {
                return attribute.charAt(0);
            }
            throw new XMLStreamException("Single character expected (read '" + ((Object) attribute) + "')");
        }

        public double getAttribute(String str, double d) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toDouble() : d;
        }

        public float getAttribute(String str, float f) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toFloat() : f;
        }

        public int getAttribute(String str, int i) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toInt() : i;
        }

        public long getAttribute(String str, long j) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toLong() : j;
        }

        public <T> T getAttribute(String str, T t) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            if (attribute == null) {
                return t;
            }
            Class<?> cls = t.getClass();
            TextFormat textFormat = TextFormat.getInstance(cls);
            if (textFormat != null) {
                return (T) textFormat.parse(attribute);
            }
            throw new XMLStreamException("No TextFormat instance for " + cls);
        }

        public String getAttribute(String str, String str2) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toString() : str2;
        }

        public CharArray getAttribute(String str) throws XMLStreamException {
            if (this._isReaderAtNext) {
                throw new XMLStreamException("Attributes should be read before reading content");
            }
            return this._reader.getAttributeValue(null, str);
        }

        public boolean getAttribute(String str, boolean z) throws XMLStreamException {
            CharArray attribute = getAttribute(str);
            return attribute != null ? attribute.toBoolean() : z;
        }

        public Attributes getAttributes() throws XMLStreamException {
            if (this._isReaderAtNext) {
                throw new XMLStreamException("Attributes should be read before content");
            }
            return this._reader.getAttributes();
        }

        public <T> T getNext() throws XMLStreamException {
            T t;
            if (hasNext()) {
                if (this._reader.getLocalName().equals(XMLFormat.NULL)) {
                    if (this._reader.next() == 2) {
                        this._isReaderAtNext = false;
                        return null;
                    }
                    throw new XMLStreamException("Non Empty Null Element");
                }
                XMLReferenceResolver xMLReferenceResolver = this._referenceResolver;
                if (xMLReferenceResolver == null || (t = (T) xMLReferenceResolver.readReference(this)) == null) {
                    try {
                        return (T) get(this._binding.getClass(this._reader.getLocalName(), this._reader.getNamespaceURI()));
                    } catch (ClassNotFoundException e) {
                        throw new XMLStreamException(e);
                    }
                } else if (this._reader.next() == 2) {
                    this._isReaderAtNext = false;
                    return t;
                } else {
                    throw new XMLStreamException("Non Empty Reference Element");
                }
            }
            throw new XMLStreamException("No more element to read", this._reader.getLocation());
        }

        public XMLStreamReader getStreamReader() {
            return this._reader;
        }

        public CharArray getText() throws XMLStreamException {
            CharArray elementText = this._reader.getElementText();
            this._isReaderAtNext = true;
            return elementText;
        }

        public boolean hasNext() throws XMLStreamException {
            if (!this._isReaderAtNext) {
                this._isReaderAtNext = true;
                this._reader.nextTag();
            }
            return this._reader.getEventType() == 1;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void reset() {
            this._binding = XMLBinding.DEFAULT;
            this._isReaderAtNext = false;
            this._reader.reset();
            this._referenceResolver = null;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void setBinding(XMLBinding xMLBinding) {
            this._binding = xMLBinding;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void setReferenceResolver(XMLReferenceResolver xMLReferenceResolver) {
            this._referenceResolver = xMLReferenceResolver;
        }
    }

    /* loaded from: classes2.dex */
    public static final class OutputElement {
        private XMLBinding _binding;
        private XMLReferenceResolver _referenceResolver;
        final XMLStreamWriterImpl _writer = new XMLStreamWriterImpl();
        private TextBuilder _tmpTextBuilder = new TextBuilder();

        /* JADX INFO: Access modifiers changed from: package-private */
        public OutputElement() {
            reset();
        }

        public void add(Object obj) throws XMLStreamException {
            if (obj == null) {
                this._writer.writeEmptyElement(XMLFormat.NULL);
                return;
            }
            Class<?> cls = obj.getClass();
            String localName = this._binding.getLocalName(cls);
            String uri = this._binding.getURI(cls);
            if (uri == null) {
                this._writer.writeStartElement(localName);
            } else {
                this._writer.writeStartElement(uri, localName);
            }
            XMLFormat<T> format = this._binding.getFormat(cls);
            if (this._referenceResolver != null && format.isReferenceable() && this._referenceResolver.writeReference(obj, this)) {
                this._writer.writeEndElement();
                return;
            }
            format.write(obj, this);
            this._writer.writeEndElement();
        }

        public void add(Object obj, String str) throws XMLStreamException {
            if (obj == null) {
                return;
            }
            this._writer.writeStartElement(str);
            Class<?> cls = obj.getClass();
            this._binding.writeClassAttribute(this._writer, cls);
            XMLFormat<T> format = this._binding.getFormat(cls);
            if (this._referenceResolver == null || !format.isReferenceable() || !this._referenceResolver.writeReference(obj, this)) {
                format.write(obj, this);
            }
            this._writer.writeEndElement();
        }

        public <T> void add(T t, String str, Class<T> cls) throws XMLStreamException {
            if (t == null) {
                return;
            }
            this._writer.writeStartElement(str);
            XMLFormat<T> format = this._binding.getFormat(cls);
            if (this._referenceResolver == null || !format.isReferenceable() || !this._referenceResolver.writeReference(t, this)) {
                format.write(t, this);
            }
            this._writer.writeEndElement();
        }

        public void add(Object obj, String str, String str2) throws XMLStreamException {
            if (obj == null) {
                return;
            }
            this._writer.writeStartElement(str2, str);
            Class<?> cls = obj.getClass();
            this._binding.writeClassAttribute(this._writer, cls);
            XMLFormat<T> format = this._binding.getFormat(cls);
            if (this._referenceResolver == null || !format.isReferenceable() || !this._referenceResolver.writeReference(obj, this)) {
                format.write(obj, this);
            }
            this._writer.writeEndElement();
        }

        public <T> void add(T t, String str, String str2, Class<T> cls) throws XMLStreamException {
            if (t == null) {
                return;
            }
            this._writer.writeStartElement(str2, str);
            XMLFormat<T> format = this._binding.getFormat(cls);
            if (this._referenceResolver == null || !format.isReferenceable() || !this._referenceResolver.writeReference(t, this)) {
                format.write(t, this);
            }
            this._writer.writeEndElement();
        }

        public void addText(CharSequence charSequence) throws XMLStreamException {
            this._writer.writeCharacters(charSequence);
        }

        public void addText(String str) throws XMLStreamException {
            this._writer.writeCharacters(str);
        }

        public XMLStreamWriter getStreamWriter() {
            return this._writer;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void reset() {
            this._binding = XMLBinding.DEFAULT;
            this._writer.reset();
            this._writer.setRepairingNamespaces(true);
            this._writer.setAutomaticEmptyElements(true);
            this._referenceResolver = null;
        }

        public void setAttribute(String str, char c) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(c));
        }

        public void setAttribute(String str, double d) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(d));
        }

        public void setAttribute(String str, float f) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(f));
        }

        public void setAttribute(String str, int i) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(i));
        }

        public void setAttribute(String str, long j) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(j));
        }

        public void setAttribute(String str, CharSequence charSequence) throws XMLStreamException {
            if (charSequence == null) {
                return;
            }
            this._writer.writeAttribute(str, charSequence);
        }

        public void setAttribute(String str, Object obj) throws XMLStreamException {
            if (obj == null) {
                return;
            }
            Class<?> cls = obj.getClass();
            TextFormat textFormat = TextFormat.getInstance(cls);
            if (textFormat != null) {
                setAttribute(str, textFormat.format((TextFormat) obj, this._tmpTextBuilder.clear()));
                return;
            }
            throw new XMLStreamException("No TextFormat instance for " + cls);
        }

        public void setAttribute(String str, String str2) throws XMLStreamException {
            if (str2 == null) {
                return;
            }
            this._writer.writeAttribute(str, str2);
        }

        public void setAttribute(String str, boolean z) throws XMLStreamException {
            setAttribute(str, (CharSequence) this._tmpTextBuilder.clear().append(z));
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void setBinding(XMLBinding xMLBinding) {
            this._binding = xMLBinding;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public void setReferenceResolver(XMLReferenceResolver xMLReferenceResolver) {
            this._referenceResolver = xMLReferenceResolver;
        }
    }

    protected XMLFormat() {
        this(null);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public XMLFormat(Class<T> cls) {
        this._class = cls;
        if (cls == null) {
            return;
        }
        synchronized (_ClassToFormat) {
            if (_ClassToFormat.containsKey(cls)) {
                throw new IllegalArgumentException("Multiple static binding for class " + cls + " The XMLFormat(Class) constructor should be used solely for static instances.");
            }
            int i = _ClassInstancesLength;
            XMLFormat[] xMLFormatArr = _ClassInstances;
            if (i >= xMLFormatArr.length) {
                XMLFormat[] xMLFormatArr2 = new XMLFormat[i * 2];
                System.arraycopy(xMLFormatArr, 0, xMLFormatArr2, 0, i);
                _ClassInstances = xMLFormatArr2;
            }
            XMLFormat[] xMLFormatArr3 = _ClassInstances;
            int i2 = _ClassInstancesLength;
            _ClassInstancesLength = i2 + 1;
            xMLFormatArr3[i2] = this;
            _ClassToFormat.put(cls, this);
        }
    }

    private static CharSequence toCsq(Object obj) {
        return Javolution.j2meToCharSeq(obj);
    }

    public final Class<T> getBoundClass() {
        return this._class;
    }

    public boolean isReferenceable() {
        return true;
    }

    public T newInstance(Class<T> cls, InputElement inputElement) throws XMLStreamException {
        try {
            return cls.newInstance();
        } catch (IllegalAccessException e) {
            throw new XMLStreamException(e);
        } catch (InstantiationException e2) {
            throw new XMLStreamException(e2);
        }
    }

    public abstract void read(InputElement inputElement, T t) throws XMLStreamException;

    public abstract void write(T t, OutputElement outputElement) throws XMLStreamException;
}
